package com.ruoyi.web.controller.erpbase;

import com.ruoyi.common.enums.SaleOrPurchType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.Cars;
import com.ruoyi.system.service.*;
import org.apache.commons.lang3.ObjectUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 开发单位：FESCO Adecco
 * 版权：FESCO Adecco
 *
 * @author：hechunbo
 * @since： JDK1.8
 * @version：1.1
 * @date：2025/8/26 最后更新日期：
 * 修改人：hechunbo
 */
public class ExecListenerPass implements ExecutionListener {


    @Override
    public void notify(DelegateExecution delegateExecution) {

        //货物对应业务模块的主表ID
        Object idMain = delegateExecution.getVariable("id");
        Long idmain = (Long)idMain;

        Object checkState = delegateExecution.getVariable("checkState");
        Integer checkstate = (Integer)checkState;

        List<Long> ids = new ArrayList<>();
        ids.add(idmain);

        //获取对应业务模块service
        // 通过Spring上下文获取Service实例
        Object serviceMethod = delegateExecution.getVariable("serviceMethod");
        if (ObjectUtils.isNotEmpty(serviceMethod)){

            String serviceMethodString = serviceMethod.toString();
            if (serviceMethodString.equals(SaleOrPurchType.SALE_PLAN.getMessage())){
                ISalePlanService salePlanService = (ISalePlanService)SpringUtils.getBean(serviceMethodString);
                salePlanService.updateApprovalState(ids,checkstate);
            } else if (serviceMethodString.equals(SaleOrPurchType.PROUCRE_PLAN.getMessage())){
                ISalePlanService salePlanService = (ISalePlanService)SpringUtils.getBean(serviceMethodString);
                salePlanService.updateApprovalState(ids,checkstate);
            } else if (serviceMethodString.equals(SaleOrPurchType.SALE_WAYBILLS.getMessage())){
                IWaybillsService waybillsService = (IWaybillsService)SpringUtils.getBean(serviceMethodString);
                waybillsService.updateApprovalState(ids,checkstate);
            } else if (serviceMethodString.equals(SaleOrPurchType.PROCURE_WAYBILLS.getMessage())){
                IWaybillsService waybillsService = (IWaybillsService)SpringUtils.getBean(serviceMethodString);
                waybillsService.updateApprovalState(ids,checkstate);
            } else if (serviceMethodString.equals(SaleOrPurchType.COLLECTION.getMessage())){
                ICollectionsService collectionsService = (ICollectionsService)SpringUtils.getBean(serviceMethodString);
                collectionsService.updateApprovalState(ids,checkstate);
            } else if (serviceMethodString.equals(SaleOrPurchType.FUNDS.getMessage())){
                IFundsService fundsService = (IFundsService)SpringUtils.getBean(serviceMethodString);
                fundsService.updateApprovalState(ids,checkstate);
            }
        }

        //可以根据 最后一级谁更新的状态 来处理 审批人和审批时间

    }
}
